package edu.gyu.myshop.controller;

import edu.gyu.myshop.model.Goods;
import edu.gyu.myshop.model.Goodstype;
import edu.gyu.myshop.model.Myuser;
import edu.gyu.myshop.service.GoodsService;
import edu.gyu.myshop.service.GoodstypeService;
import edu.gyu.myshop.service.MyuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller

public class MyuserController {
    @Autowired
    GoodstypeService goodstypeService;

    @Autowired
    GoodsService goodsService;

    @RequestMapping({"/", ""})
    public String start(HttpSession session, Model model) {
        List<Goodstype> goodstypes=goodstypeService.list();

        List<Goods> goods=goodsService.lambdaQuery()
                .eq(Goods::getIsrecommend,1).list();

        List<Goods> goodsList=goodsService.lambdaQuery()
                .eq(Goods::getIsNew,1).list();
        session.setAttribute("types", goodstypes);
        model.addAttribute("newGoods", goodsList);
        model.addAttribute("recommendGoods", goods);

        return "/index";
    }

    @Autowired
    MyuserService myuserService;

    @RequestMapping({"/toLogin"})
    public String toLogin() {
        return "/login";
    }

    @PostMapping("/dologin")
    public String dologin(Myuser myuser, HttpServletRequest request) {
        // 通过用户名查询用户
        Myuser user = myuserService.lambdaQuery()
                .eq(Myuser::getUsername, myuser.getUsername())
                .one();

        // 初始化密码编码器
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        if (user != null && passwordEncoder.matches(myuser.getPassword(), user.getPassword())) {
            // 如果用户存在且密码匹配，则将密码清空并存入session
            user.setPassword("");
            request.getSession().setAttribute("user", user);
            return "/";
        } else {
            // 如果用户名或密码错误，返回登录页面并提示错误信息
            String msg = "用户名或密码错误";
            request.setAttribute("msg", msg);
            return "/login";
        }
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "/";
    }

    @RequestMapping({"/toReg"})
    public String reg() {
        return "/reg";
    }

    @PostMapping("/doReg")
    public String doReg(Myuser myuser, HttpServletRequest request) {
        Myuser user = myuserService.lambdaQuery()
                .eq(Myuser::getUsername, myuser.getUsername())
                .one();
        if (user != null) {
            String msg = "用户名被占用！";
            request.setAttribute("msg", msg);
            request.setAttribute("isSuccess", false);
        } else {
            // 对密码进行加密
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encodedPassword = passwordEncoder.encode(myuser.getPassword());
            myuser.setPassword(encodedPassword);
            myuser.setCreateTime(LocalDateTime.now());
            myuserService.save(myuser);
            String msg = "恭喜" + myuser.getUsername() + "注册成功，请";
            request.setAttribute("msg", msg);
            request.setAttribute("isSuccess", true);
        }

        return "/reg";
    }
}